<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-11-11
 * Time: 9:24
 */

namespace App\Http\Controllers\Mobile;


use App\Http\Controllers\Controller;
use App\Model\Account\Administration;
use App\Model\Backstage\Customers;
use Illuminate\Http\Request;
use App\Model\Backstage\User;
use App\Tool\Error;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;

class PersonalController extends Controller
{
    private $arr = ['JPG', 'JPEG', 'PNG', 'GIF', 'jpg', 'jpeg', 'png', 'gif'];


    //个人信息
    public function personalInfo(Request $request){
        if (!$request->input(['uid'])) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $res = $customers->getPersonalInfo($request->input(['uid']));
        if(!$res){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$res);
    }

    //个人头像上传
    public function personalAvatar(Request $request)
    {
        if (!$request->input('uid')) {
            return $this->error(Error::PARAM_MISSING);
        }
        $file = $request->file('file');
        if ($file->isValid()) {
            $suffixname = $file->getClientOriginalExtension();
            if (in_array($suffixname, $this->arr)){
                $filePath =  $file->getRealPath();
                $uniqid = md5(uniqid());
                //上传七牛云的文件名称
                $nameHost = date('YmdHis',time()).$uniqid . '.' . $suffixname;
                $auth = new Auth(env('QINIU_ACCESSKEY'), env('QINIU_SECRETKEY'));
                $token = $auth->uploadToken(env('QINIU_BUCKET'));
                $uploadMgr = new UploadManager();
                $temp_arr = [
                    'color' => 1
                ];
                list($ret, $err) = $uploadMgr->putFile($token, $nameHost, $filePath,$temp_arr);

                if ($err !== null) {
                    //上传七牛云存储失败
                    return $this->error(Error::UPLOAD_ERROR);
                } else {
                    //上传成功 返回图片路径
                    $data = [
                        'preview_url' => env('QINIU_DOMAIN').$ret['key'],
                        'add_url'     => $ret['key']
                    ];

                    $res = User::editAvatar($request->input('uid'), $data['preview_url']);
                    if (!$res){
                        return $this->error(Error::UPLOAD_ERROR);
                    }
                    $avatar = $request->input('avatar');
                    if($avatar){
                        $avatar = basename($avatar);
                        $administration = new Administration();
                        $administration->delqiniuimg($avatar);
                    }
                    return $this->success('ok', $data);
                }
            }else{
                //文件格式错误
                return $this->error(Error::UPLOAD_SUFFIX_ERROR);
            }
        }else{
            //文件不合法
            return $this->error(Error::FILE_ERROR);
        }
    }


}